package com.barneyx.mshinfoplatform.service.admin.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.barneyx.mshinfoplatform.entity.admin.AdminRoleEntity;
import com.barneyx.mshinfoplatform.entity.admin.AdminUserRoleMapEntity;
import com.barneyx.mshinfoplatform.exception.AdminUserException;
import com.barneyx.mshinfoplatform.mapper.admin.IAdminRoleEntityMapper;
import com.barneyx.mshinfoplatform.mapper.admin.IAdminUserRoleMapEntityMapper;
import com.barneyx.mshinfoplatform.service.admin.IAdminRoleEntityService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;

@Service
public class AdminRoleEntityServiceImpl extends ServiceImpl<IAdminRoleEntityMapper, AdminRoleEntity> implements IAdminRoleEntityService {
    @Resource
    private IAdminRoleEntityMapper adminRoleMapper;
    @Resource
    private IAdminUserRoleMapEntityMapper admin_user_role_map_mapper;

    @Override
    public boolean deleteRoleById(Serializable roleId) throws AdminUserException {
        AdminRoleEntity adminRoleEntity = adminRoleMapper.selectById(roleId);
        if (adminRoleEntity.getIsSys())
            throw new AdminUserException("删除失败!当前删除的分组为系统级分组,请解除系统级选项以后再进行删除");
        Long aLong = admin_user_role_map_mapper.selectCount(new LambdaQueryWrapper<AdminUserRoleMapEntity>().eq(AdminUserRoleMapEntity::getRoleId, roleId));
        if (aLong > 0)
            throw new AdminUserException("删除失败！此分组下还有关联的管理员用户，请处理完该分组下的管理员用户以后再尝试删除！");
        adminRoleMapper.deleteRoleById(roleId);
        return true;
    }
}
